package com.ruoyi.service.jpjs.rlexportdata.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.jpjs.PublicMapper;
import com.ruoyi.mapper.jpjs.attendance.AttendanceMapper;
import com.ruoyi.mapper.jpjs.empchange.EmpChangeMapper;
import com.ruoyi.mapper.jpjs.emponjob.EmpOnJobMapper;
import com.ruoyi.mapper.jpjs.empquit.EmpQuitMapper;
import com.ruoyi.mapper.jpjs.recruitment.RecruitmentMapper;
import com.ruoyi.mapper.jpjs.socialpayment.SocialPaymentMapper;
import com.ruoyi.service.jpjs.rlexportdata.RlExportDataOneService;
import com.ruoyi.service.jpjs.rlexportdata.RlExportDataService;
import com.ruoyi.service.jpjs.rlexportdata.RlExportDataThreeService;
import com.ruoyi.service.jpjs.rlexportdata.RlExportDataTowService;
import com.ruoyi.system.domain.SysUser;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

@Service
public class RlExportDataServiceImpl implements RlExportDataService {
    @Autowired
    PublicMapper publicMapper;
    @Autowired
    RlExportDataOneService rlExportDataOneService;
    @Autowired
    RlExportDataTowService rlExportDataTowService;
    @Autowired
    RlExportDataThreeService rlExportDataThreeService;

    @Override
    public AjaxResult exportData(String years, String months) {
        try {
            HSSFWorkbook wb = new HSSFWorkbook();

            SysUser user = ShiroUtils.getSysUser();
            List<Map<String, Object>> maps = publicMapper.findUserCompAndJszb(user.getUserId());

            //人员异动
            rlExportDataOneService.exportEmpChange(wb, years, months, maps);
            //在职人员分析
            rlExportDataOneService.exportEmpOnJob(wb, years, months, maps);
            //离职人员分析
            rlExportDataOneService.exportEmpQuit(wb, years, months, maps);
            //招聘
            rlExportDataTowService.exportRecruitment(wb, years, months, maps);
            //考勤
            rlExportDataTowService.exportAttendance(wb, years, months, maps);
            //合同社保公积金分析
            rlExportDataTowService.exportSocialPayment(wb, years, months, maps);
            //人力资源成本分析
            rlExportDataThreeService.exportHumanResources(wb, years, months, maps);
            //产值利润费用分析
            rlExportDataThreeService.exportCzlrfy(wb, years, months, maps);
            //员工持证情况分析
            rlExportDataThreeService.exportEmpCertificate(wb, years, months, maps);
            String filePath = Global.getDownloadPath() + "人力资源数据.xls";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wb.write(fileOut);
            fileOut.close();
            return AjaxResult.success(0);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("失败");
        }
    }
}